package com.heima.wemedia.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.comment.dtos.CommentRepayDto;
import com.heima.model.comment.pojos.ApComment;
import com.heima.model.comment.pojos.ApCommentRepay;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.ThreadLocalUtils;
import com.heima.wemedia.mapper.WmCommentRepayMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmCommentRepayService;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author: Xu
 * @Date: 2022/01/05/17:11
 */
@Service
@Transactional
public class WmCommentRepayServiceImpl extends ServiceImpl<WmCommentRepayMapper, ApCommentRepay> implements WmCommentRepayService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private WmUserMapper wmUserMapper;
    /**
     * 保存回复评论
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveCommentRepay(CommentRepayDto dto) {
        String commentId = dto.getCommentId();
        String content = dto.getContent();
        if (StringUtils.isEmpty(content)){
            throw new LeadNewsException(444,"评论内容不能为空");
        }
        if (StringUtils.isEmpty(commentId)){
            throw new LeadNewsException(AppHttpCodeEnum.AD_USER_DATA_NOT_EXIST);
        }
        WmUser wmUser = (WmUser) ThreadLocalUtils.get();
        if (wmUser == null){
            throw new LeadNewsException(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据自媒体用户ID 用户对应的作者ID 以及作者姓名
        Integer wmUserId = wmUser.getId();
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getId,wmUserId);
        WmUser user = wmUserMapper.selectOne(wrapper);
        //自定义封装评论回复对象
        ApCommentRepay apCommentRepay = new ApCommentRepay();
//        apCommentRepay.setId();
        apCommentRepay.setAuthorId(user.getApAuthorId());
        apCommentRepay.setAuthorName(user.getName());
        apCommentRepay.setCommentId(dto.getCommentId());
        apCommentRepay.setContent(dto.getContent());
        apCommentRepay.setLikes(0);
        apCommentRepay.setCreatedTime(new Date());
        apCommentRepay.setUpdatedTime(new Date());
        mongoTemplate.save(apCommentRepay);

        return ResponseResult.okResult(200,"评论回复成功");
    }
}
